<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼盘名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="楼盘名称"></el-input>
    </el-form-item>
    <el-form-item label="省份id" prop="aid1">
      <el-input v-model="dataForm.aid1" placeholder="省份id"></el-input>
    </el-form-item>
    <el-form-item label="市Id" prop="aid2">
      <el-input v-model="dataForm.aid2" placeholder="市Id"></el-input>
    </el-form-item>
    <el-form-item label="区id" prop="aid3">
      <el-input v-model="dataForm.aid3" placeholder="区id"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="area">
      <el-input v-model="dataForm.area" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="site">
      <el-input v-model="dataForm.site" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="售楼处地址" prop="sell">
      <el-input v-model="dataForm.sell" placeholder="售楼处地址"></el-input>
    </el-form-item>
    <el-form-item label="均价 (单位: 平方米)" prop="averagePrice">
      <el-input v-model="dataForm.averagePrice" placeholder="均价 (单位: 平方米)"></el-input>
    </el-form-item>
    <el-form-item label="开盘时间" prop="crearedate">
      <el-input v-model="dataForm.crearedate" placeholder="开盘时间"></el-input>
    </el-form-item>
    <el-form-item label="开盘状态(1: 已开盘  0:未开盘)" prop="housesState">
      <el-input v-model="dataForm.housesState" placeholder="开盘状态(1: 已开盘  0:未开盘)"></el-input>
    </el-form-item>
    <el-form-item label="售楼电话" prop="telphone">
      <el-input v-model="dataForm.telphone" placeholder="售楼电话"></el-input>
    </el-form-item>
    <el-form-item label="开发商" prop="developersId">
      <el-input v-model="dataForm.developersId" placeholder="开发商"></el-input>
    </el-form-item>
    <el-form-item label="装修情况" prop="fitment">
      <el-input v-model="dataForm.fitment" placeholder="装修情况"></el-input>
    </el-form-item>
    <el-form-item label="销售状态" prop="marketState">
      <el-input v-model="dataForm.marketState" placeholder="销售状态"></el-input>
    </el-form-item>
    <el-form-item label="楼盘类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="楼盘类型"></el-input>
    </el-form-item>
    <el-form-item label="楼盘图片" prop="images">
      <el-input v-model="dataForm.images" placeholder="楼盘图片"></el-input>
    </el-form-item>
    <el-form-item label="楼盘VR视频" prop="video">
      <el-input v-model="dataForm.video" placeholder="楼盘VR视频"></el-input>
    </el-form-item>
    <el-form-item label="楼盘介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="楼盘介绍"></el-input>
    </el-form-item>
    <el-form-item label="物业公司" prop="rea">
      <el-input v-model="dataForm.rea" placeholder="物业公司"></el-input>
    </el-form-item>
    <el-form-item label="物业类型" prop="realType">
      <el-input v-model="dataForm.realType" placeholder="物业类型"></el-input>
    </el-form-item>
    <el-form-item label="物业费" prop="realPrice">
      <el-input v-model="dataForm.realPrice" placeholder="物业费"></el-input>
    </el-form-item>
    <el-form-item label="建筑面积" prop="architecture">
      <el-input v-model="dataForm.architecture" placeholder="建筑面积"></el-input>
    </el-form-item>
    <el-form-item label="车位情况" prop="stall">
      <el-input v-model="dataForm.stall" placeholder="车位情况"></el-input>
    </el-form-item>
    <el-form-item label="属性值" prop="property">
      <el-input v-model="dataForm.property" placeholder="属性值"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          aid1: '',
          aid2: '',
          aid3: '',
          area: '',
          site: '',
          sell: '',
          averagePrice: '',
          crearedate: '',
          housesState: '',
          telphone: '',
          developersId: '',
          fitment: '',
          marketState: '',
          type: '',
          images: '',
          video: '',
          introduce: '',
          rea: '',
          realType: '',
          realPrice: '',
          architecture: '',
          stall: '',
          property: ''
        },
        dataRule: {
          name: [
            { required: true, message: '楼盘名称不能为空', trigger: 'blur' }
          ],
          aid1: [
            { required: true, message: '省份id不能为空', trigger: 'blur' }
          ],
          aid2: [
            { required: true, message: '市Id不能为空', trigger: 'blur' }
          ],
          aid3: [
            { required: true, message: '区id不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          site: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          sell: [
            { required: true, message: '售楼处地址不能为空', trigger: 'blur' }
          ],
          averagePrice: [
            { required: true, message: '均价 (单位: 平方米)不能为空', trigger: 'blur' }
          ],
          crearedate: [
            { required: true, message: '开盘时间不能为空', trigger: 'blur' }
          ],
          housesState: [
            { required: true, message: '开盘状态(1: 已开盘  0:未开盘)不能为空', trigger: 'blur' }
          ],
          telphone: [
            { required: true, message: '售楼电话不能为空', trigger: 'blur' }
          ],
          developersId: [
            { required: true, message: '开发商不能为空', trigger: 'blur' }
          ],
          fitment: [
            { required: true, message: '装修情况不能为空', trigger: 'blur' }
          ],
          marketState: [
            { required: true, message: '销售状态不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '楼盘类型不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '楼盘图片不能为空', trigger: 'blur' }
          ],
          video: [
            { required: true, message: '楼盘VR视频不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '楼盘介绍不能为空', trigger: 'blur' }
          ],
          rea: [
            { required: true, message: '物业公司不能为空', trigger: 'blur' }
          ],
          realType: [
            { required: true, message: '物业类型不能为空', trigger: 'blur' }
          ],
          realPrice: [
            { required: true, message: '物业费不能为空', trigger: 'blur' }
          ],
          architecture: [
            { required: true, message: '建筑面积不能为空', trigger: 'blur' }
          ],
          stall: [
            { required: true, message: '车位情况不能为空', trigger: 'blur' }
          ],
          property: [
            { required: true, message: '属性值不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`//houses/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.houses.name
                this.dataForm.aid1 = data.houses.aid1
                this.dataForm.aid2 = data.houses.aid2
                this.dataForm.aid3 = data.houses.aid3
                this.dataForm.area = data.houses.area
                this.dataForm.site = data.houses.site
                this.dataForm.sell = data.houses.sell
                this.dataForm.averagePrice = data.houses.averagePrice
                this.dataForm.crearedate = data.houses.crearedate
                this.dataForm.housesState = data.houses.housesState
                this.dataForm.telphone = data.houses.telphone
                this.dataForm.developersId = data.houses.developersId
                this.dataForm.fitment = data.houses.fitment
                this.dataForm.marketState = data.houses.marketState
                this.dataForm.type = data.houses.type
                this.dataForm.images = data.houses.images
                this.dataForm.video = data.houses.video
                this.dataForm.introduce = data.houses.introduce
                this.dataForm.rea = data.houses.rea
                this.dataForm.realType = data.houses.realType
                this.dataForm.realPrice = data.houses.realPrice
                this.dataForm.architecture = data.houses.architecture
                this.dataForm.stall = data.houses.stall
                this.dataForm.property = data.houses.property
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`//houses/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'aid1': this.dataForm.aid1,
                'aid2': this.dataForm.aid2,
                'aid3': this.dataForm.aid3,
                'area': this.dataForm.area,
                'site': this.dataForm.site,
                'sell': this.dataForm.sell,
                'averagePrice': this.dataForm.averagePrice,
                'crearedate': this.dataForm.crearedate,
                'housesState': this.dataForm.housesState,
                'telphone': this.dataForm.telphone,
                'developersId': this.dataForm.developersId,
                'fitment': this.dataForm.fitment,
                'marketState': this.dataForm.marketState,
                'type': this.dataForm.type,
                'images': this.dataForm.images,
                'video': this.dataForm.video,
                'introduce': this.dataForm.introduce,
                'rea': this.dataForm.rea,
                'realType': this.dataForm.realType,
                'realPrice': this.dataForm.realPrice,
                'architecture': this.dataForm.architecture,
                'stall': this.dataForm.stall,
                'property': this.dataForm.property
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
